package com.xintoucloud.pbs.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xintoucloud.pbs.entity.PartyMemberPermission;
import com.xintoucloud.pbs.entity.PartyMemberPermissionMenu;
import com.xintoucloud.pbs.model.DataGrid;
import com.xintoucloud.pbs.model.Param;
import com.xintoucloud.pbs.model.TreeData;
import com.xintoucloud.pbs.service.MenuService;
import com.xintoucloud.pbs.service.PartyMemberPermissionMenuService;
import com.xintoucloud.pbs.service.PartyMemberPermissionService;

@Controller@RequestMapping("/partyMemberPermission")
public class PartyMemberPermissionController {
	@Autowired
	private PartyMemberPermissionService partyMemberPermissionService;
	@Autowired
	private PartyMemberPermissionMenuService partyMemberPermissionMenuService;
	@Autowired
	private MenuService menuService;
	@RequestMapping("/list")
	public String list() {
		return "listPartyMemberPermission";
	}
	@ResponseBody
	@RequestMapping("/listAll")
	public DataGrid<PartyMemberPermission> listAll(HttpServletRequest request){
		//1.获取参数
		 String page = request.getParameter("page");// 获取当前页 
		 String size = request.getParameter("limit");// 获取每页信息显示的条数 
		 int pages = Integer.parseInt(page); 
		 int rows = Integer.parseInt(size);  
		 // 查询总数 
		DataGrid<PartyMemberPermission> pmperms=partyMemberPermissionService.listPartyMemberPermission(pages, rows);
		return pmperms;
	}
	@RequestMapping("/add")
	public String add(PartyMemberPermission partyMemberPermission){
		partyMemberPermissionService.addPartyMemberPermission(partyMemberPermission);
		return "redirect:list";
	}
	@ResponseBody
	@PostMapping("/delete")
	public PartyMemberPermission delete(int ppid,Model m) {
		partyMemberPermissionService.deletePartyMemberPermission(ppid);
		PartyMemberPermission data=partyMemberPermissionService.getPartyMemberPermission(ppid);
		return data;
	}
	@PostMapping("/update")
	public String update(int oid,int state,HttpSession session) {
		int ppid=(int) session.getAttribute("ppid");
		partyMemberPermissionMenuService.updateState(ppid, oid, state);
		return "redirect:list";
	}
	@ResponseBody
	@PostMapping("/updateBefore")
	public Param updateBefore(int oid,HttpSession session) {
		Param param=new Param();
		int ppid=(int) session.getAttribute("ppid");
		PartyMemberPermissionMenu ppm=partyMemberPermissionMenuService.getState(ppid, oid);
		param.setState(ppm.getState()+"");
		param.setLevel("1");
		return param;
	}
	@GetMapping("/menuList")
	public String menuList(int ppid,HttpSession session) {
		session.setAttribute("ppid", ppid);
		return "menuListTree";
	}
	@ResponseBody
	@PostMapping("/menuList2")
	public TreeData menuList2(HttpSession session) {
		int ppid= (int) session.getAttribute("ppid");
		System.out.println("我执行了"+ppid);
		TreeData treeData=menuService.listTreeDataByParentId(0,ppid);
		return treeData;
	}
}
